﻿/**************************************************************
*作者：Leon
*创建时间：2022/4/5 16:21:02
**************************************************************/
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace Lad.DrawProject.Controls;

public class RotateThumb : Thumb
{
    private IBaseCanvasViewModel? serviceProvider;

    public IBaseCanvasViewModel ServiceProvider
    {
        get { return serviceProvider ??= (IBaseCanvasViewModel)this.GetBaseCanvas()!.DataContext; }
    }

    private Point centerPoint;
    private Vector startVector;
    private double initialAngle;
    private Canvas? designerCanvas;
    private DesignerItemViewModelBase? designerItem;

    public RotateThumb()
    {
        DragDelta += RotateThumb_DragDelta;
        DragStarted += RotateThumb_DragStarted;
        DragCompleted += RotateThumb_DragCompleted;
    }

    private List<SelectableItemViewModelBase>? designerItems;

    private void RotateThumb_DragStarted(object sender, DragStartedEventArgs e)
    {
        if (this.DataContext is DesignerItemViewModelBase designer)
        {
            this.designerItem = designer;
            designerItems = ServiceProvider.SelectedItems.ToList();

            foreach (var item in designerItems)
                item.SetOldValue(this.designerItem.Angle, nameof(this.designerItem.Angle));

            this.designerCanvas = this.GetBaseCanvas();
            if (this.designerCanvas != null)
            {
                this.centerPoint =
                    new Point(this.designerItem.Left + this.designerItem.Width * 0.5,
                              this.designerItem.Top + this.designerItem.Height * 0.5);

                Point startPoint = Mouse.GetPosition(this.designerCanvas);
                this.startVector = Point.Subtract(startPoint, this.centerPoint);


                this.initialAngle = this.designerItem.Angle;
            }
            e.Handled = true;
        }
        else
        {
            designerItems = null;
        }
    }

    private void RotateThumb_DragCompleted(object sender, DragCompletedEventArgs e)
    {
        this.designerItem = this.DataContext as DesignerItemViewModelBase;

        if (this.designerItems != null)
        {
            foreach (DesignerItemViewModelBase item in designerItems.OfType<DesignerItemViewModelBase>())
            {
                item.RaiseAngle();
            }

            e.Handled = true;
        }
    }

    private void RotateThumb_DragDelta(object sender, DragDeltaEventArgs e)
    {
        if (this.designerItems != null && this.designerCanvas != null)
        {
            Point currentPoint = Mouse.GetPosition(this.designerCanvas);
            Vector deltaVector = Point.Subtract(currentPoint, this.centerPoint);

            double angle = Vector.AngleBetween(this.startVector, deltaVector);

            foreach (DesignerItemViewModelBase item in designerItems.OfType<DesignerItemViewModelBase>())
            {
                item.Angle = this.initialAngle + Math.Round(angle, 0);
            }
            e.Handled = true;
        }
    }


}
